// components/verification_code_tool/verification_code_tool.js
const mybehavior = require("../../behaviors/requests")
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [mybehavior],
  properties: {
    show_id:{
      type:Boolean,
      value:true
    },
  },

  /**
   * 组件的初始数据
   */
  data: {

    base_urlstr: "https://logisticsengineering.gree.com",
    yam_word: "发送验证码",
    count: 0,
    yzm_btn_style: "color: #aeaeff;",
    phone_num: "",
    ver_code: "",
    ID_number: "",
    name: "",
    captcha: "",
    yzm_show: false,

  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached: function () {
      this.setData({
        phone_num: wx.getStorageSync('phone_num'),
        ID_number: wx.getStorageSync('ID_number'),
        name: wx.getStorageSync('name'),

      })
      // 在组件实例进入页面节点树时执行
      if (this.data.phone_num != "") {

        this.setData({
          yzm_btn_style: "color: #5555FF;"

        })
      }






    },

  },
  methods: {
    checkLoginBtn(e) {
      let key = e.currentTarget.dataset.key
      let key_data = e.detail

      if (this.data.phone_num != "") {
        this.setData({
          yzm_btn_style: "color: #5555FF;"

        })
      } else {
        this.setData({
          yzm_btn_style: "color: #aeaeff;"

        })
      }

      this.triggerEvent('getGeneralData', {
        [key]: key_data
      })
    },
    // 发送验证码
    sentCode() {

      if (this.data.phone_num == "") {
        wx.showToast({
          title: "手机号不能为空",
          icon: "error"
        })
      } else {

        var url = this.data.base_urlstr + "/gm_user/gm_get_verification_code2";
        var data = {
          captcha: this.data.captcha,
          phone_num: this.data.phone_num,
        }
        console.log("发送验证码", data)
        this.post(url, data).then(res => {
          console.log("发送验证码返回信息", res)
          if (res.code == 20000) {
            this.setData({
              yzm_show: false,
              captcha: "",
              // yzm_show: true,
              yam_btn_disabled: true,
              yam_word: 60,
              count: 0,
            })
            this.sentBtn()

            wx.showToast({
              title: res.msg,
              icon: "success"
            });
          } else {

            wx.showToast({
              title: res.msg,
              icon: "error"
            })
          }
        }).catch(err => {

          wx.showToast({
            title: err.msg,
            icon: 'none'
          })
        })
      }


    },

    // 发送按钮倒计时
    sentBtn() {
      var a = this.data.count
      var t = this.data.yam_word
      if (a < 60) {
        this.setData({
          yam_word: t - 1,
          count: a + 1,
        })
        setTimeout(() => {
          this.sentBtn()
        }, 1000)
      } else {
        this.setData({
          yam_word: "发送验证码"
        })

      }

    },
    // 获取图形验证码
    BerforeSentCode() {

      wx.showLoading({
        title: '加载中....',
      })
      var url = this.data.base_urlstr + "/gm_user/gm_get_captcha";
      var data = {
        phone_num: this.data.phone_num,
      }
      let self = this

      wx.request({
        url: url,
        data: data,
        responseType: 'arraybuffer',
        success(res) {

          if (res.statusCode == 200) {
            const arrayBuffer = res.data;
            // 将 ArrayBuffer 转为 Base64
            const base64 = wx.arrayBufferToBase64(arrayBuffer);
            // 直接用作图片源（需后台返回完整Base64带前缀）
            self.setData({
              imageSrc: 'data:image/png;base64,' + base64,
              yzm_show: true,
            });

          } else {
            wx.showToast({
              title: '手机号有误，请重新确认！',
              icon: 'none'
            })
          }

          wx.hideLoading()

        }
      });



    },

    onCloseLogin() {
      this.setData({
        yzm_show: false,
        captcha: ""
      })
    },
    onConfirmLogin() {
      if (this.data.captcha == "") {
        wx.showToast({
          title: '请输入图形验证码！',
          icon: 'none'
        })
      } else {

        this.sentCode()
      }
    },
  }
})
